<?php
class Backend extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library(array('session', 'pagination'));
        $this->load->helper(array('url', 'form', 'date'));
        $this->load->database();
        $ok = $this->session->userdata('admin');
        if (!isset($ok) || $ok != 'ok') {
            redirect("Login/index");
        }
    }

    function index(){
        $category_id=$this->uri->segment(3);
        $data['category_id']=$category_id;
        $this->load->view("backend/head");

        $this->load->view("backend/left",$data);
        $this->load->view("backend/index",$data);
        $this->load->view("backend/foot",$data);
    }
    function publishNews(){
        $category_id=$this->uri->segment(3);
        $data['category_id']=$category_id;
        $this->load->view("backend/head");

        $this->load->view("backend/left",$data);
        $this->load->view("backend/news",$data);
        $this->load->view("backend/foot",$data);
    }

    function institutionList(){
        $category_id=$this->uri->segment(3);
        $data['category_id']=$category_id;
        $this->load->view("backend/head");

        $this->load->view("backend/left",$data);
        $this->load->view("backend/institutionList",$data);
        $this->load->view("backend/foot",$data);
    }

    function newsInformList(){
        $category_id=$this->uri->segment(3);
        $data['category_id']=$category_id;
        $this->load->model("BackendModel");

        $config['total_rows'] = $this->BackendModel->get_newsInform_rows($category_id);
        $config['base_url'] = base_url() . 'index.php/backend/newsInformList/' . $category_id;
        $config['num_links'] = '3';
        $config['uri_segment'] = '4';
        $links = intval($this->uri->segment(4));// intval保证sql语句中limit的默认限制是0
        $config['per_page'] = '8';
        $config['first_link'] = '首页';
        $config['last_link'] = '末页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $result = $this->BackendModel->get_category($category_id);
        $data['category'] = $result->name;
        $this->pagination->initialize($config);
        $data['article'] = $this->BackendModel->get_newsInformList($category_id, $links, $config['per_page']);
        $data['now_num']=$links;
        $this->load->view("backend/head");
        $this->load->view("backend/left",$data);
        $this->load->view("backend/NewsInformList",$data);
        $this->load->view("backend/foot",$data);
    }

    function edit_newsInform(){
        $category_id=$this->uri->segment(3);
        $id=$this->uri->segment(4);
        $this->load->model("BackendModel");
        $data['article']=$this->BackendModel->get_newsInfo($id);
        $result = $this->BackendModel->get_category($category_id);
        $data['category_id']=$category_id;
        $data['category'] = $result->name;
        $this->load->view("backend/head");
        $this->load->view("backend/left",$data);
        if($category_id==3||$category_id==5){
            $this->load->view("backend/edit_news",$data);
        }elseif($category_id==4||$category_id==6){
            $this->load->view("backend/edit_inform",$data);
        }
        $this->load->view("backend/foot",$data);
    }

    function topNewsInform(){
        $type = $this->uri->segment(3);
        $id = $this->uri->segment(4);
        $top = $this->uri->segment(5);
        $this->load->model("BackendModel");
        $res = $this->BackendModel->top_newsInform($top,$id);
        redirect(site_url("Backend/newsInformList")."/".$type);
    }
    function check_edit_news(){
        $this->load->model("BackendModel");
        $category_id=$this->input->post("category_id");
        $data['category_id']=$category_id;
        $this->BackendModel->edit_news();
        redirect("backend/newsInformList/$category_id");
    }

    function add_news(){
        $category_id=$this->uri->segment(3);
        $this->load->model("BackendModel");
        $result = $this->BackendModel->get_category($category_id);
        $data['category_id']=$category_id;
        $data['category'] = $result->name;
        $this->load->view("backend/head");
        $this->load->view("backend/left",$data);
        if($category_id==3||$category_id==5){
            $this->load->view("backend/news",$data);
        }elseif($category_id==4||$category_id==6){
            $this->load->view("backend/inform",$data);
        }
        $this->load->view("backend/foot",$data);
    }

    function check_add_news(){
        $this->load->model("BackendModel");
        $category_id=$this->input->post("category_id");
        $data['category_id']=$category_id;
        $this->BackendModel->add_news($category_id);
        redirect("backend/newsInformList/$category_id");
    }

    function inform(){
        $category_id=$this->uri->segment(3);
        $data['category_id']=$category_id;
        $this->load->view("backend/head");
        $this->load->view("backend/left",$data);
        $this->load->view("backend/inform",$data);
        $this->load->view("backend/foot",$data);
    }

    function deleteNewsInform(){
        $type = $this->uri->segment(3);
        $id = $this->uri->segment(4);
        $this->load->model("BackendModel");
        $res = $this->BackendModel->delete_NewsInform($type,$id);
        if($res){
            redirect(site_url("Backend/newsInformList")."/".$type);
        }else{
            echo "出错了！";
        }
    }

    function getSchoolAssistant(){
        $data["type"] = "辅导员";$data["wPower"] = "A";
        $this->load->model("BackendModel");
        $data["arr_schoolAssistant"] = $this->BackendModel->get_schoolAssistant();
        $data["rows"] = count($data["arr_schoolAssistant"]);
        $data["flag"] = ceil($data["rows"]/2);
        $category_id=$this->uri->segment(3);
        $data['category_id']=$category_id;
        $this->load->view("backend/head");
        $this->load->view("backend/left",$data);
        $this->load->view("backend/institutionList",$data);
        $this->load->view("backend/foot",$data);
    }

    function getSchoolTeacher(){
        $data["type"] = "教师";$data["wPower"] = "T";
        $this->load->model("BackendModel");
        $data["arr_schoolAssistant"] = $this->BackendModel->get_schoolTeacher();
        $data["rows"] = count($data["arr_schoolAssistant"]);
        $data["flag"] = ceil($data["rows"]/2);
        $category_id=$this->uri->segment(3);
        $data['category_id']=$category_id;
        $this->load->view("backend/head");
        $this->load->view("backend/left",$data);
        $this->load->view("backend/institutionList",$data);
        $this->load->view("backend/foot",$data);
    }

    function getSchoolStudent(){
        $data["type"] = "学生";
        $this->load->model("BackendModel");
        $data["arr_schoolStudent"] = $this->BackendModel->get_schoolStudent();
        $data["rows"] = count($data["arr_schoolStudent"]);
        $data["flag"] = ceil($data["rows"]/2);

        //记录状态
        $category_id=$this->uri->segment(3);
        $data['category_id']=$category_id;
        $this->load->view("backend/head");
        $this->load->view("backend/left",$data);
        $this->load->view("backend/institutionList",$data);
        $this->load->view("backend/foot",$data);
    }

    //获取成员列表
    function getDetailInformation(){
        $judge = $this->uri->segment(3);
        //echo $judge;exit();
        //$institution = $this->input->post("institution");$this->session->set_userdata("institution",$institution);
        $institution = urldecode($this->uri->segment(4));
        if($judge==13||$judge==14||$judge==15){
            $institution = $this->session->userdata("wInstitution");
        }
        $this->load->model("BackendModel");
        $school = $this->session->userdata("wSchool");

        //装载类文件
        $this->load->library('pagination');
        //每一页显示的数据条数的变量
        $page_size=10;

        $this->load->helper('url');//分页一定要用它！！！！！！
        //$config['base_url']=site_url('user/test/crowprince');//随便加一个
        //$config['uri_segment']=4;
        $config['base_url']=site_url("Backend/getDetailInformation/$judge")."/".urlencode($institution);
        $config['uri_segment']=5;//分页的偏移量查询在那一段上面

        //每一页显示的数据条数
        $config['per_page']=$page_size;
        $config['first_link']= '1';
        $config['next_link']= '下一页';
        $config['prev_link']= '上一页';
        $config['last_link']= '2';
        $offset=intval($this->uri->segment(5));//用intval使空格转0，显示出来0
        $data["offset"] = $offset;
        if($judge==9||$judge==10||$judge==13||$judge==14){
            $data["judge"] = $judge;
            if($judge==9||$judge==13){
                $wPower = "A";
            }else{
                $wPower = "T";
            }

            $data["arr_worker"] = $this->BackendModel->get_schoolWorkerInformation($school,$institution,$wPower,$offset,$page_size);
            //var_dump($data["arr_worker"]);exit();
            $sql = "select * from workers where wSchool='$school'and wInstitution='$institution' and wPower='$wPower'";
            $res = $this->db->query($sql);
            $rows = $res->num_rows();
        }else{
            $data["judge"] = $judge;
            $sMajor = $this->uri->segment(5);
            $config['base_url']=site_url("Backend/getDetailInformation/$judge")."/".urlencode($institution)."/".$sMajor;
            $config['uri_segment']=6;//分页的偏移量查询在那一段上面
            $offset=intval($this->uri->segment(6));//用intval使空格转0，显示出来0
            $data["offset"] = $offset;
            $sMajor = urldecode($sMajor);
            //echo $sMajor.$institution;
            $data['arr_student'] = $this->BackendModel->get_schoolStudentInformation($school,$institution,$sMajor,$offset,$page_size);
            //var_dump($data["arr_student"]);exit();
            $sql = "select * from students where sSchool='$school'and sInstitution='$institution'";
            $res = $this->db->query($sql);
            $rows = $res->num_rows();
        }
        $config['total_rows']=$rows;
        //初始化 ，传入配置？？？？
        $this->pagination->initialize($config);
        $data['links'] = $this->pagination->create_links();

        //记录状态
        $category_id=$this->uri->segment(3);
        $data['category_id']=$category_id;
        $this->load->view("backend/head");
        $this->load->view("backend/left",$data);
        $this->load->view("backend/tSList",$data);
        $this->load->view("backend/foot",$data);
    }

    function deleteMember(){
        $institution = $this->uri->segment(4);
        $id = $this->uri->segment(6);
        $judge = $this->uri->segment(3);
        $offset = $this->uri->segment(5);
        if($judge==11||$judge==15){
            $sql = "delete from students where id=$id";
        }else if($judge==9||$judge==10||$judge==13||$judge==14){
            $sql = "delete from workers where id=$id";
        }
        $this->db->query($sql);
        redirect(site_url("Backend/getDetailInformation/$judge/$institution/$offset"));
    }

    //通过ajax获取学校学生专业
    function getMajor(){
        $sInstitution = $this->input->post("institution");
        $institution = urlencode($sInstitution);
        $sSchool = $this->session->userdata("wSchool");
        $this->load->model("BackendModel");
        $data["sMajor"] = $this->BackendModel->get_Major($sSchool,$sInstitution);
        $output="";
        foreach($data["sMajor"] as $item){
            $output=" <div class=\"col-md-6 col-md-offset-3\">
                       <div class=\"list-group\">
                           <a href=\"".site_url('Backend/getDetailInformation/11/').'/'.$institution.'/'.urlencode($item["sMajor"])."\" class=\"list-group-item btn btn-default\">".$item["sMajor"]."</a>
                       </div>
                    </div>";
        }

        echo $output;
    }
    //获取院系学生专业
    function getMajor2(){
        $sInstitution = $this->session->userdata("wInstitution");
        $institution = urlencode($sInstitution);
        $sSchool = $this->session->userdata("wSchool");
        $this->load->model("BackendModel");
        $data["sMajor"] = $this->BackendModel->get_Major($sSchool,$sInstitution);
        $category_id=$this->uri->segment(3);
        $data['category_id']=$category_id;
        $this->load->view("backend/head");
        $this->load->view("backend/left",$data);
        $this->load->view("backend/majorForStudents",$data);
        $this->load->view("backend/foot",$data);
    }

    function updateMyInformation(){
        $category_id=$this->uri->segment(3);
        $data['category_id']=$category_id;
        $workerId=$this->session->userdata("workerId");
        $this->load->model("BackendModel");
        $data['workerInfo']=$this->BackendModel->getWorkerInfo($workerId);
        $this->load->view("backend/head");
        $this->load->view("backend/left",$data);
        $this->load->view("backend/myProfile",$data);
        $this->load->view("backend/foot",$data);
    }

    function check_old_pwd()
    {
        $password=md5($this->input->post("password"));
        $workerId=$this->session->userdata("workerId");
        $this->load->model("BackendModel");
        $data['workerInfo']=$this->BackendModel->getWorkerInfo($workerId);
        foreach($data['workerInfo'] as $value){
            $old_pwd = $value->wPassword;
        }
        if($password==$old_pwd){
            echo "1";
        }else{
            echo "0";
        }
    }

    //修改旧密码
    function updateOldPassword()
    {
        $id=$this->session->userdata("workerId");
        if(isset($id)){
            $this->load->model("BackendModel");
            $this->BackendModel->updatePwd($id);
            redirect("Backend/updateMyInformation/1");
        }else{
            die( "修改失败，登录超时，请重新登录！");
        }

    }

    function ajaxPage()
    {
        $institution = $this->session->userdata("institution");
        $school = $this->session->userdata("school");
        $wPower = $this->session->userdata("power");
        $this->load->model('BackendModel');
        $offset=$this->input->post('offset');//通过ajax获取当前第几页
        //$article_id=$this->input->post('article_id');//通过ajax获取文章的id
        $cur_page=$this->input->post('cur_page');//通过ajax获取当前第几页
        $config['base_url'] = site_url("Backend/getSchoolAssistant/9");
        $sql = "select * from workers where wSchool='$school'and wInstitution='$institution' and wPower='$wPower'";
        $res = $this->db->query($sql);
        $rows = $res->num_rows();
        $config['total_rows'] = $rows;
        $config['per_page'] = 1;
        //$config['full_tag_open'] = '<p class=pagestyle>';//这里便于为页码加样式
        //$config['full_tag_close'] = '</p>';
        $config['next_link'] = 'next';
        $config['prev_link'] = 'prev';
        $config['use_page_numbers']='true';//使用页码，而不是offset
        $config['anchor_class']="class='ajax_fPage'";//借鉴第一篇文章的大神，这里为每个a标签加样式
        $config['cur_page']=$cur_page;
        $this->my_pagination->initialize($config);//默认的对象名是类名的小写
        $links=$this->my_pagination->create_links($cur_page);
        $arr = $this->Mhome->get_ajaxPage($offset,$config['per_page'],$school,$institution,$wPower);
        $output = "<table class=\"table text-center\">
							<caption style=\"margin-bottom: 30px;\">
								<span class=\"glyphicon glyphicon-list-alt\"></span>&nbsp&nbsp<span style=\"font-weight: bold;font-size: 15px\">成员列表</span>
							</caption>
							<thead>
								<tr style=\"font-weight:bold;\"><td>#</td><td>学校</td><td>院系</td><td>姓名</td><td>工号</td><td>操作</td></tr>
							</thead>
							<tbody class=\".table-striped\">";

        foreach($arr as $item){
            $output .="<tr><td>1</td><td>河南大学</td><td>计算机与信息工程学院</td><td>李贤</td><td>123456</td><td>
									<table>
										<tr>
										<td width=\"44%\"></td>
										<td width=\"30%\"></td>
										<td><a href=\"#\"><span class=\"glyphicon glyphicon-trash\" style=\"color:red;\"></span></a></td>
										</tr>
									</table>
								</td></tr>";
        }

        $output .="<td colspan=\"5\">
									<nav>
									  <ul class=\"pagination\">
										<li>".$links."</li>
									  </ul>
									</nav>
									</td>
								</tr>
							</tbody>
						</table>";
        echo $output;
    }

}